/**
 * Created by wangguoqing on 2017/11/8.
 */

var width = $(window).width();
var height = $(window).height();

$(function () {
    $("#div_3level_block").hide();
    $("#div_4level_block").hide();
    getSubMenu(menuCode, 2);
});

function changeClass(id) {
    var len = $("#" + id + " span").size();
    var arr = [];
    for (var index = 0; index < len; index++) {//创建一个数字数组
        arr[index] = index;
    }
    $.each(arr, function (i) {//循环得到不同的id的值
        var idValue = $("#" + id + " span").eq(i).attr("id");
        if (idValue != '') {
            $("#" + idValue).removeClass("sel-1");
            $("#" + idValue).removeClass("sel");
        }
    });
}

function openMenu(code, level, lb) {
    var div_id = '';
    if (level == 3) {
        div_id = 'div_4level';
    } else {
        div_id = 'div_3level';
        $("#div_4level_block").hide();
    }

    var params = new Object();
    params.menuCode = code;
    $.ajax({
        type: "POST",
        url: path + "/system/register/queryByCode",
        data: params,
        dataType: "json",
        success: function callback(data) {
            var analyMsg = data;
            if (analyMsg.code != "0000") {
                if ((analyMsg.code == "9999")) {
                    $.messager.alert("操作提示", analyMsg.msg, "warning", function () {
                        var top = window;
                        while (top != top.parent) {
                            top = top.parent;
                        } //获取当前页面的顶层窗口对象
                        top.location.href = "/system/register/toLogin"; //跳转到登陆页面
                    });
                }
            } else {
                if (lb != '1') {
                    if (analyMsg.returnObject['menuUrl'] != '' && analyMsg.returnObject['menuUrl'] != null) {
                        parent.addTab(analyMsg.returnObject['menuName'], analyMsg.returnObject['menuUrl'], "", analyMsg.returnObject['menuCode']);
                    } else {
                        $("#" + div_id + "_block").show();
                        getSubMenu(code, level + 1);
                    }
                } else {
                    if (analyMsg.returnObject['menuUrl'] != '' && analyMsg.returnObject['menuUrl'] != null) {
                        $("#" + div_id + "_block").hide();
                    } else {
                        $("#" + div_id + "_block").show();
                        getSubMenu(code, level + 1);
                    }
                }
            }
        },
        error: function (data, textstatus) {
            $.alert("default", "error", data.responseText);
        }
    });
}

function getSubMenu(code, level) {
    var div_id = '';
    var title_id = '';
    if (level == 3) {
        div_id = 'div_3level';
        title_id = 'title_3level';
    } else if (level == 4) {
        div_id = 'div_4level';
        title_id = 'title_4level';
    } else {
        div_id = 'div_2level';
        title_id = 'title_2level';
    }

    var params = new Object();
    params.parentMenuCode = code;
    $.ajax({
        type: "POST",
        url: path + "/system/register/getSubMenu",
        dataType: "json",
        data: params,
        success: function callback(data) {
            var analyMsg = data;
            if (!(analyMsg.code == "0000")) {
                //$.messager.alert("友情提醒",analyMsg.msg,"info",function(){});
                $.alert("default", "info", analyMsg.msg);
            } else {
                var s = '<ol>';
                $("#" + title_id).html(analyMsg.returnObject.dto["menuName"]);
                for (var i = 0; i < analyMsg.returnObject.rows.length; i++) {
                    var parentId = analyMsg.returnObject.rows[i]["parentId"];
                    var menuName = analyMsg.returnObject.rows[i]["menuName"];
                    var menuCode = analyMsg.returnObject.rows[i]["menuCode"];
                    var menuCode_n = menuCode + ',' + level;

                    s = s + '<li><a class="' + analyMsg.returnObject.rows[i]['moduleIcon'] + '" href="javascript:openMenu(' + menuCode_n + ')"><span id="';
                    s = s + menuCode + '">' + analyMsg.returnObject.rows[i]['menuDescription'] + '</span></a></li>';

                }
                s = s + '<div class="clearfix"></div>';
                s = s + '</ol>';
                document.getElementById(div_id).innerHTML = s;

                var len = $("#div_" + level + "level" + " span").length;
                var arr = [];
                for (var index = 0; index < len; index++) {//创建一个数字数组
                    arr[index] = index;
                }
                $.each(arr, function (i) {//循环得到不同的id的值
                    var idValue = $("#div_" + level + "level" + " span").eq(i).attr("id");
                    if (idValue != '') {
                        $("#" + idValue).hover(function () {
                            openMenu(idValue, level, '1');
                        }, function () {

                        });
                    }
                });

            }
        }
    });

}